﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public interface IPagedList<T>
{

    int PageIndex { get; }

    int PageSize { get; }

    int TotalCount { get; }

    int TotalPages { get; }

    bool HasPreviousPage { get; }

    bool HasNextPage { get; }

}

public class PagedList<T> : List<T>, IPagedList<T>
{

    public PagedList(IQueryable<T> source, int pageIndex, int pageSize)
    {

        int total = source.Count();

        this.TotalCount = total;

        this.TotalPages = total / pageSize;

        if (total % pageSize > 0)

            TotalPages++;

        this.PageSize = pageSize;

        this.PageIndex = pageIndex;

        this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());

    }

    public PagedList(List<T> source, int pageIndex, int pageSize)
    {

        int total = source.Count();

        this.TotalCount = total;

        this.TotalPages = total / pageSize;

        if (total % pageSize > 0)

            TotalPages++;

        this.PageSize = pageSize;

        this.PageIndex = pageIndex;

        this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());

    }

    public int PageIndex { get; private set; }

    public int PageSize { get; private set; }

    public int TotalCount { get; private set; }

    public int TotalPages { get; private set; }

    public bool HasPreviousPage
    {

        get { return (PageIndex > 0); }

    }

    public bool HasNextPage
    {

        get { return (PageIndex + 1 < TotalPages); }

    }
}
